package cn.lyjuan.android.demofive.ui.slide.load;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import cn.lyjuan.android.demofive.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chad on 2016/3/27.
 */
public class SlideUpLoadActivity extends Activity
{
    private SlideUpLoadListView listView;
    private List<String> list;
    private ArrayAdapter<String> adp;

    public static final int HANDLER_WHAT_LOAD_MORE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.ui_slide_load_up_load);

        initComponemt();
        initData();
    }

    private void initData()
    {
        list = new ArrayList<>(20);
        for (int i = 0; i < 20; i++)
        {
            list.add(Integer.toString(i));
        }

        adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        listView.setAdapter(adp);
        adp.notifyDataSetChanged();

        this.listView.setLoader(loader);
    }

    private void initComponemt()
    {
        listView = (SlideUpLoadListView) this.findViewById(R.id.ui_slide_load_up_load);
    }

    public interface LoadMore
    {
        void loadMore();
    }

    private LoadMore loader = new LoadMore()
    {
        @Override
        public void loadMore()
        {
            new Handler().postDelayed(new Runnable()
            {
                @Override
                public void run()
                {
                    for (int i = 0; i < 5; i++)
                    {
                        SlideUpLoadActivity.this.list.add(Integer.toString(SlideUpLoadActivity.this.list.size()));
                    }

                    SlideUpLoadActivity.this.listView.onLoadComplete();
                }
            }, 5000);
        }
    };
}
